//
//  ViewController.m
//  test
//
//  Created by jcc on 16/6/1.
//  Copyright © 2016年 jcc. All rights reserved.
//

#import "ViewController.h"
#import "MBProgressHUD.h"
#import "JCCSectionModel.h"
#import "JCCHeaderView.h"
#import "FSCTint.h"
#import "JYSlideSegmentController.h"
#import "JCCNetworking.h"
#import "AFNetworking.h"

static NSString *kCellIdentfier = @"UITableViewCell";
static NSString *kHeaderIdentifier = @"HeaderView";
#define RootUrl @"http://121.40.55.132:8282/api-ptep/api"
#define LOGIN @"/user/login"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *sectionDataSources;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:self.tableView];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.tableView registerClass:[UITableViewCell class]
           forCellReuseIdentifier:kCellIdentfier];
    [self.tableView registerClass:[JCCHeaderView class] forHeaderFooterViewReuseIdentifier:kHeaderIdentifier];
    
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"设置" style:UIBarButtonItemStylePlain target:self action:@selector(showRightView)];
    
    self.navigationItem.rightBarButtonItem = rightButton;

}
- (NSMutableArray *)sectionDataSources {
    if (_sectionDataSources == nil) {
        _sectionDataSources = [[NSMutableArray alloc] init];
        
        for (NSUInteger i = 0; i < 20; ++i) {
            JCCSectionModel *sectionModel = [[JCCSectionModel alloc] init];
            sectionModel.isOpen = NO;
            sectionModel.sectionTitle = [NSString stringWithFormat:@"section: %ld", i];
            NSMutableArray *itemArray = [[NSMutableArray alloc] init];
            for (NSUInteger j = 0; j < 10; ++j) {
                JCCcellModel *cellModel = [[JCCcellModel alloc] init];
                cellModel.cellTitle = [NSString stringWithFormat:@"section=%ld, row=%ld", i, j];
                [itemArray addObject:cellModel];
            }
            sectionModel.cellModel = itemArray;
            
            [_sectionDataSources addObject:sectionModel];
        }
    }
    
    return _sectionDataSources;
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.sectionDataSources.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    JCCSectionModel *sectionModel = self.sectionDataSources[section];
    
    return sectionModel.isOpen ? sectionModel.cellModel.count : 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentfier
                                                            forIndexPath:indexPath];
    JCCSectionModel *sectionModel = self.sectionDataSources[indexPath.section];
    JCCcellModel *cellModel = sectionModel.cellModel[indexPath.row];
    cell.textLabel.text = cellModel.cellTitle;
    
    return cell;
}

#pragma mark - UITableView Delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSMutableArray *vcs = [NSMutableArray array];
    for (int i = 0; i < 5; i++) {

        UIViewController *vc = [[UIViewController alloc] init];
        vc.title = [NSString stringWithFormat:@"%d", i];
        if (i % 2 == 0) {
            vc.view.backgroundColor = [UIColor blueColor];
        } else {
        }
        [vcs addObject:vc];
    }
    JYSlideSegmentController *targeVC = [[JYSlideSegmentController alloc] initWithViewControllers:vcs];
    targeVC.title = @"JYSlideSegmentController";
    targeVC.indicatorInsets = UIEdgeInsetsMake(0, 8, 0, 8);
    targeVC.indicatorColor = [UIColor blueColor];
    targeVC.itemWidth = 80;
    [self.navigationController pushViewController:targeVC animated:YES];
    
    
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    JCCHeaderView *view = [tableView dequeueReusableHeaderFooterViewWithIdentifier:kHeaderIdentifier];
    
    JCCSectionModel *sectionModel = self.sectionDataSources[section];
    view.model = sectionModel;
    view.ExpandCallback = ^(BOOL isOpen) {
        [tableView reloadSections:[NSIndexSet indexSetWithIndex:section]
                 withRowAnimation:UITableViewRowAnimationFade];
    };
    
    return view;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 44;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 44;
}

- (void)showRightView{
    [JCCNetworking setBaseUrl:@"http://112.124.97.108:8082"];
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithDictionary:@{@"username":@"jcc",
                                   @"password":@"123456",
                                       @"type":@"",
                                     @"openid":@"" }];
    [JCCNetworking postWithUrl:@"/ssoLogin" params:params success:^(id response) {
        NSLog(@"%@",response);
    } fail:^(NSError *error) {
        NSLog(@"%@",error);
    }];

    
}
- (void)show{
    NSLog(@"show");
}
@end
